layui.use(['form', 'jquery', 'jquery_cookie'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        $ = layui.jquery_cookie($);


    form.on('submit(login)', function (data) {

        console.log(data.field); //当前容器的全部表单字段，名值对形式：{nage:value}

        //阻止表单跳转，如果需要表单跳转，去掉这段即可
        $.ajax({
            type: "post",
            url: ctx + "/user/login",
            data: {
                userName: data.field.username,
                userPwd: data.field.password,
            },
            success: function (result) {
                //code == 200 成功。
                if (result.code == 200) {
                    //登录成功
                    layer.msg("登录成功", function () {
                        if ($("#rememberMe").prop("checked")) {

                            //将用户存入cookie中,设置7天时效
                            $.cookie("userIdStr", result.result.userIdStr, {expires: 7});
                            $.cookie("userName", result.result.userName, {expires: 7});
                            $.cookie("trueName", result.result.trueName, {expires: 7});
                        } else {

                            //将用户存入cookie中
                            $.cookie("userIdStr", result.result.userIdStr);
                            $.cookie("userName", result.result.userName);
                            $.cookie("trueName", result.result.trueName);
                        }
                        //跳转首页
                        window.location.href = ctx + "/main";
                    });
                } else {
                    layer.msg(result.msg, {icon: 5})
                }
            }
        });
        return false;
    });

});